package com.company.dcm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.company.dcm.common.R;
import com.company.dcm.entity.R_ClothSheet;
import com.company.dcm.entity.R_DeviceInfo;
import com.company.dcm.service.DevService;
import com.company.dcm.service.R_ClothSheetService;
import com.company.dcm.service.R_DeviceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 实时查询
 */
@Slf4j
@RestController
@RequestMapping("/RealTime")
public class RealTimeController {
    @Autowired
    private R_DeviceInfoService rDeviceInfoService;
    @Autowired
    private DevService devService;

    @GetMapping("/R_DeviceInfo")
    public R<R_DeviceInfo> getR_DeviceInfo(String deviceCode){
        R_DeviceInfo deviceInfo = rDeviceInfoService.getLastR_DeviceInfoByDeviceCode(deviceCode);
        return R.success(deviceInfo);
    }

    @GetMapping("/R_DeviceInfoWorkShop")
    public R<List<R_DeviceInfo>> getR_DeviceInfoWorkShop(Long id){
        List<R_DeviceInfo> result = new ArrayList<>();
        List<String> deviceCodes = devService.getDeviceCodeByWorkShopId(id);
        for (String deviceCode: deviceCodes) {
            R_DeviceInfo deviceInfo = rDeviceInfoService.getLastR_DeviceInfoByDeviceCode(deviceCode);
            result.add(deviceInfo);
        }

        return R.success(result);
    }

    @GetMapping("/R_DeviceInfoFactory")
    public R<List<R_DeviceInfo>> getR_DeviceInfoFactory(Long factoryId){
        List<R_DeviceInfo> result = new ArrayList<>();
        List<String> deviceCodes = devService.getDeviceCodeByFactoryId(factoryId);
        for (String deviceCode: deviceCodes) {
            R_DeviceInfo deviceInfo = rDeviceInfoService.getLastR_DeviceInfoByDeviceCode(deviceCode);
            result.add(deviceInfo);
        }

        return R.success(result);
    }


    @GetMapping("/R_DeviceInfoList")
    public R<List<R_DeviceInfo>> getR_DeviceInfoList(Long factoryId,Long id){
        List<R_DeviceInfo> list = new ArrayList<>();
        R_DeviceInfo deviceInfo;
        List<String> result = new ArrayList<>();
        //1.1按车间查
        if(id!=null)result=devService.getDeviceCodeByWorkShopId(id);
        //1.2按厂房查
        else if(factoryId!=null)result=devService.getDeviceCodeByFactoryId(factoryId);
        //1.3都没有按权限查
        else result= devService.getDeviceCodeByAuth();
        System.out.println(result);
        for (String a:result){
            deviceInfo=rDeviceInfoService.getLastR_DeviceInfoByDeviceCode(a);
            if(deviceInfo!=null){
                list.add(deviceInfo);
            }
        }
        return R.success(list);
    }
}
